package com.dyz.wsw;

import com.dyz.wsw.common.consts.cache.PassportCacheConst;
import com.dyz.wsw.common.pojo.auth.Principal;
import com.dyz.wsw.common.utils.converter.Json;
import com.dyz.wsw.domain.admin.repository.cache.IUserCacheRepository;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.PipedReader;
import java.util.ArrayList;

/**
 * 测试Redis服务相关
 * @author dyz
 * Time 2024/7/3 17:59
 */
@SpringBootTest
public class testRedis implements PassportCacheConst {

    @Autowired
    IUserCacheRepository iUserCacheRepository;

    @Autowired
    private ReactiveRedisTemplate<String,Object> redis;

    @Test
    public void get() throws IOException {
        Gson gson = new Gson();
        redis.opsForHash().get("test","222")
                .flatMap(json->{
                    Principal principal = gson.fromJson(json.toString(), Principal.class);
                    System.out.println("Principal: "+principal);
                    return Mono.just(principal);
                })
                .subscribe(System.out::println);
        System.in.read();
    }


    @Test
    public void save() throws IOException {
        Principal principal = new Principal();
        principal.setId(1L);
        principal.setUsername("ADMIN");
        Gson gson = new Gson();
        redis.opsForHash().put("test","222",gson.toJson(principal))
                        .subscribe(System.out::println);
        System.in.read();
    }




    /**
     * redis设置用户状态
     * @throws IOException
     */
    @Test
    public void setUserState() throws IOException {

        iUserCacheRepository.setUserDisabled(1L)
                        .subscribe(System.out::println);
        System.in.read();
    }

    /**
     * redis获取用户禁用状态
     * @throws IOException
     */
    @Test
    public void getUserState() throws IOException{
        iUserCacheRepository.getUserState(2L)
                        .subscribe(System.out::println);
        System.in.read();
    }

    /**
     * redis用户状态续期
     * @throws IOException
     */
    @Test
    public void doRenew() throws IOException {
        iUserCacheRepository.renewal(1L)
                        .subscribe(System.out::println);
        System.in.read();
    }

}
